package org.example.deep.utils.json;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

public class ResultJson implements Serializable {
    //0:失败 1：成功 2：token已过期 3：无权限 4：ip限制访问

    private int code = 0;

    private String msg;

    private Object content;

    private boolean success = false;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public ResultJson fail(int resultCode, String resultMessage) {
        this.msg = resultMessage;
        this.code = resultCode;
        this.content = "";
        this.success = false;
        return this;
    }

    public ResultJson fail(String resultMessage) {
        this.msg = resultMessage;
        this.code = 0;
        this.content = "";
        this.success = false;
        return this;
    }

    public ResultJson success() {
        this.msg = "成功";
        this.code = 1;
        this.content = "";
        this.success = true;
        return this;
    }

    public ResultJson success(Object data) {
        this.msg = "成功";
        this.code = 1;
        this.content = data;
        this.success = true;
        return this;
    }

    public ResultJson success(String resultMessage, Object data) {
        this.msg = resultMessage;
        this.code = 1;
        this.content = data;
        this.success = true;
        return this;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this, new MyValueFilter(), SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    }
}
